<?php

require_once("controllers/init.php");
require_once("model/classes/events/EventsResultSet.php");
require_once("model/classes/events/ICalendar.php");


$results = new EventsResultSet();
$from = new DateTime("now");
$from->modify("-1 month");

$to = new DateTime("3000-01-01");

$results->setTo($to);
$results->setFrom($from);

$message = new Message();
$dtNow = new DateTime("now");
$message->setFrom("St Nicholas Activity Planning<no-reply@stnicholascathedralmusic.co.uk>");
$message->addRecipient('michael@michaelstoddart.org');
$message->setSubject("SNAP calendar accessed on ".$dtNow->format("F j, Y"));
$message->send();




// girls involvee id = 3
//$involveeID = 3;

$sql = "SELECT DISTINCT Ev.* FROM Events as Ev ";
//$sql = "SELECT DISTINCT Ev.*,Inv.EventInvolveeNameID FROM Events as Ev ";
//$sql .= "INNER JOIN EventInvolvees AS Inv ON Ev.EventID = Inv.EventID ";
$sql .= "WHERE Ev.Start > '".$from->format("Y-m-d H:i")."' ";
$sql .= "AND Ev.DeletedBy = 0 ";
//$sql .= "AND Inv.EventInvolveeNameID = ".$involveeID." ";
$sql .= "ORDER BY Ev.Start,Ev.EventID";

// echo $sql."\n\n";

$results->setSql($sql);


$calendar = new ICalendar();
$calendar->setEventsResultSet($results);
$calendar->showComment(false);
$calendar->showLocations(true);
$calendar->showInvolvees(false);
$calendar->getCalendar();
		






/*
<?php
require_once("controllers/init.php");
require_once("model/classes/events/EventsResultSet.php");


function output_string_protected($string) 
{
    return htmlspecialchars(trim($string));
}  

header('Content-type: text/calendar; charset=utf-8');
header("Content-Disposition: inline; filename=snap.ics");


echo "BEGIN:VCALENDAR\n";
echo "METHOD:PUBLISH\n";
echo "PRODID:SNAP\n";
echo "VERSION:2.0\n";

echo "BEGIN:VTIMEZONE\n";
echo "TZID:Europe/London\n";
echo "BEGIN:DAYLIGHT\n";
echo "TZOFFSETFROM:+0000\n";
echo "TZOFFSETTO:+0100\n";
echo "DTSTART:19810329T010000\n";
echo "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\n";
echo "TZNAME:BSTr\n";
echo "END:DAYLIGHT\n";
echo "BEGIN:STANDARD\n";
echo "TZOFFSETFROM:+0100\n";
echo "TZOFFSETTO:+0000\n";
echo "DTSTART:19961027T020000\n";
echo "RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\n";
echo "TZNAME:GMT\n";
echo "END:STANDARD\n";
echo "END:VTIMEZONE\n";

/
$results = new EventsResultSet();
$results->setFrom(new DateTime("2013-11-01"));
$results->setTo(new DateTime("3000-01-01"));
$events = $results->getEvents();

//print_r($events)

$message = new Message();
$dtNow = new DateTime("now");
$message->setFrom("St Nicholas Activity Planning<no-reply@stnicholascathedralmusic.co.uk>");
$message->addRecipient('michael@michaelstoddart.org');
$message->setSubject("SNAP calendar accessed on ".$dtNow->format("F j, Y"));
$message->send();


foreach($events as $event)

{
	echo "BEGIN:VEVENT\n";
	echo "CLASS:PUBLIC\n";
	echo "UID:" . md5(uniqid(mt_rand(), true)) . "@stnicholascathedralmusic.co.uk\n";
	echo "CREATED:" . $event->getCreatedWhen()->format('Ymd\THis') . "\n";
	echo "SUMMARY:" . $event->getTitle() . "\n";
	$description = $event->getDescription()." - ".$event->getComment()." [".$event->getInvolveesList()."] [".$event->getLocationsList()."]";
	$description = str_ireplace('<br>', ', ', $description);
	echo "DESCRIPTION:".$description."\n";
	$timenow = new DateTime('now');
	echo "DTSTAMP:" . $timenow->format('Ymd\THis') . "\n";
	echo "DTEND;TZID=Europe/London:" . $event->getEnd()->format('Ymd\THis') . "\n";
	echo "DTSTART;TZID=Europe/London:" . $event->getStart()->format('Ymd\THis') . "\n";
//	echo "LOCATION:" . $Qdata->valueProtected('Location') . "\n";
	echo "END:VEVENT\n";
}

echo "END:VCALENDAR";
 ?>
 
 * 
 * */
 